package business_opportunity

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/common/http_client"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/constants"
	businessOpportunityDto "git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/dto/business_opportunity"
	pb "git.mycaigou.com/gfyx/micro-gfyx-mp-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	"git.myscrm.cn/golang/stark/v4"
)

type customerRepository struct {
}

// NewCustomerRepository
// 找商机
func NewCustomerRepository() CustomerRepositoryIface {
	return &customerRepository{}
}

// GetAllOptions 获取全部备选项
func (r *customerRepository) GetAllOptions(ctx context.Context, params []string) (interface{}, error) {
	v := map[string]interface{}{}
	data := map[string]interface{}{}
	data["tags"] = params
	path := "/pub/40220901/optionsPub/getAllOptions"
	// todo tenantCode
	header := map[string]string{}
	_, _, errs := http_client.NewGfyxModelApiHttpClient().Post(ctx, path, data, header).EndStruct(&v)
	if len(errs) > 0 {
		return nil, errs[0]
	}

	return v, nil
}

// GetFeeds 获取开发商动态
func (r *customerRepository) GetFeeds(ctx context.Context, params map[string]interface{}) (interface{}, error) {
	v := map[string]interface{}{}

	path := "/pub/40220901/developerPub/GetFeeds"
	// todo tenantCode
	header := map[string]string{}
	_, _, errs := http_client.NewGfyxModelApiHttpClient().Post(ctx, path, params, header).EndStruct(&v)
	if len(errs) > 0 {
		return nil, errs[0]
	}

	return v, nil
}

// TakeLand 拿地列表
func (r *customerRepository) TakeLand(ctx context.Context, params map[string]interface{}) (interface{}, error) {
	v := map[string]interface{}{}
	path := "/pub/40220901/developerPub/takeLand"

	header := map[string]string{}
	_, _, errs := http_client.NewGfyxModelApiHttpClient().Post(ctx, path, params, header).EndStruct(&v)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	//处理建模接口的error
	errJm := http_client.NewGfyxModelApiHttpClient().GetJmError(v)
	if errJm != nil {
		return nil, errJm
	}

	return v, nil
}

// OpenedTrading 开盘列表
func (r *customerRepository) OpenedTrading(ctx context.Context, params map[string]interface{}) (interface{}, error) {
	v := map[string]interface{}{}
	path := "/pub/40220901/developerPub/openedTrading"

	header := map[string]string{}
	_, _, errs := http_client.NewGfyxModelApiHttpClient().Post(ctx, path, params, header).EndStruct(&v)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	//处理建模接口的error
	errJm := http_client.NewGfyxModelApiHttpClient().GetJmError(v)
	if errJm != nil {
		return nil, errJm
	}

	return v, nil
}

// CompanyOption 公司下拉选项
func (r *customerRepository) CompanyOption(ctx context.Context, params map[string]interface{}) (interface{}, error) {
	v := map[string]interface{}{}
	path := "/pub/40220901/developerPub/companyOption"

	header := map[string]string{}
	_, _, errs := http_client.NewGfyxModelApiHttpClient().Post(ctx, path, params, header).EndStruct(&v)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	//处理建模接口的error
	errJm := http_client.NewGfyxModelApiHttpClient().GetJmError(v)
	if errJm != nil {
		return nil, errJm
	}

	return v, nil
}

// CheckDeveloperMainData 校验开发商是否在主档案
func (r *customerRepository) CheckDeveloperMainData(ctx context.Context, params map[string]interface{}) (interface{}, error) {
	v := map[string]interface{}{}
	path := "/pub/40220901/developerPub/checkDeveloperMainData"

	header := map[string]string{}
	_, _, errs := http_client.NewGfyxModelApiHttpClient().Post(ctx, path, params, header).EndStruct(&v)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	//处理建模接口的error
	errJm := http_client.NewGfyxModelApiHttpClient().GetJmError(v)
	if errJm != nil {
		return nil, errJm
	}

	return v, nil
}

// ApplyDeveloperMainData 申请反哺开发商主档案接口
func (r *customerRepository) ApplyDeveloperMainData(ctx context.Context, params map[string]interface{}) (interface{}, error) {
	v := map[string]interface{}{}
	path := "/pub/40220901/developerPub/applyDeveloperMainData"

	header := map[string]string{}
	_, _, errs := http_client.NewGfyxModelApiHttpClient().Post(ctx, path, params, header).EndStruct(&v)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	//处理建模接口的error
	errJm := http_client.NewGfyxModelApiHttpClient().GetJmError(v)
	if errJm != nil {
		return nil, errJm
	}

	return v, nil
}

func (r *customerRepository) GetDeveloperScoreDetail(ctx context.Context, organizingCode string) (*businessOpportunityDto.CustomerScore, error) {
	request := &pb.GetCustomerScoreRequest{
		OrganizingCode: organizingCode,
	}

	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewCustomerServiceClient(conn)
	res, err := client.GetCustomerScore(ctx, request)
	if err != nil {
		return nil, err
	}
	if res == nil {
		return nil, errors.New("获取好客户得分失败")
	}
	customerScore := &businessOpportunityDto.CustomerScore{
		OrganizingCode: res.OrganizingCode,
		TotalScore:     res.TotalScore,
		UpdatedTime:    res.UpdatedTime,
	}
	if res.InvestmentAmount != nil {
		customerScore.InvestmentAmount = &businessOpportunityDto.ScoreDetail{
			Score:    res.InvestmentAmount.Score,
			MaxScore: res.InvestmentAmount.MaxScore,
			Detail:   res.InvestmentAmount.Detail,
		}
	}
	if res.ReturnOfFund != nil {
		customerScore.ReturnOfFund = &businessOpportunityDto.ScoreDetail{
			Score:    res.ReturnOfFund.Score,
			MaxScore: res.ReturnOfFund.MaxScore,
			Detail:   res.ReturnOfFund.Detail,
		}
	}
	if res.CreditScore != nil {
		customerScore.CreditScore = &businessOpportunityDto.ScoreDetail{
			Score:    res.CreditScore.Score,
			MaxScore: res.CreditScore.MaxScore,
			Detail:   res.CreditScore.Detail,
		}
	}
	if res.MatchDemand != nil {
		customerScore.MatchDemand = &businessOpportunityDto.ScoreDetail{
			Score:    res.MatchDemand.Score,
			MaxScore: res.MatchDemand.MaxScore,
			Detail:   res.MatchDemand.Detail,
		}
	}
	if res.AnnualIncrease != nil {
		customerScore.AnnualIncrease = &businessOpportunityDto.ScoreDetail{
			Score:    res.AnnualIncrease.Score,
			MaxScore: res.AnnualIncrease.MaxScore,
			Detail:   res.AnnualIncrease.Detail,
		}
	}
	return customerScore, nil
}

func (r *customerRepository) GetSupplierProject(ctx context.Context, request *pb.GetSupplierProjectRequest) (*pb.GetSupplierProjectResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "SupplierProject-error: %+v, request: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewCustomerServiceClient(conn)
	res, err := client.GetSupplierProject(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "SupplierProject-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}

func (r *customerRepository) GetFeedList(ctx context.Context, request *pb.GetFeedRequest) (*pb.GetFeedResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetFeedList-error: %+v, request: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewCustomerServiceClient(conn)
	res, err := client.GetFeed(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetFeedList-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}

func (r *customerRepository) GetInvestCompany(ctx context.Context, request *pb.GetInvestCompanyRequest) (*pb.GetInvestCompanyResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetInvestCompany-error: %+v, request: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewCustomerServiceClient(conn)
	res, err := client.GetInvestCompany(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetInvestCompany-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}

func (r *customerRepository) SearchCustomerList(ctx context.Context, request *pb.SearchCustomerListRequest) (*pb.SearchCustomerListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "SearchCustomerList request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewCustomerServiceClient(conn)
	res, err := client.SearchCustomerList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "SearchCustomerList request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (r *customerRepository) GetCustomerDetail(ctx context.Context, request *pb.GetCustomerDetailRequest) (*pb.GetCustomerDetailResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCustomerDetail request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewCustomerServiceClient(conn)
	res, err := client.GetCustomerDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCustomerDetail request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (r *customerRepository) FuzzySearch(ctx context.Context, request *pb.FuzzySearchCustomerRequest) (*pb.FuzzySearchCustomerResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "FuzzySearch request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewCustomerServiceClient(conn)
	res, err := client.FuzzySearchCustomer(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "FuzzySearch request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetCustomerZxList 企业风险-失信被执行
func (r *customerRepository) GetCustomerZxList(ctx context.Context, request *pb.GetCustomerRiskCommonRequest) (*pb.GetCustomerZxListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "FuzzySearch request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewCustomerServiceClient(conn)
	res, err := client.GetCustomerZxList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "FuzzySearch request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetCustomerCourtRegister 企业风险-立案信息
func (r *customerRepository) GetCustomerCourtRegister(ctx context.Context, request *pb.GetCustomerRiskCommonRequest) (*pb.GetCustomerCourtRegisterResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "FuzzySearch request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewCustomerServiceClient(conn)
	res, err := client.GetCustomerCourtRegister(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "FuzzySearch request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetCustomerLawSuit 企业风险-法律诉讼
func (r *customerRepository) GetCustomerLawSuit(ctx context.Context, request *pb.GetCustomerRiskCommonRequest) (*pb.GetCustomerLawSuitResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "FuzzySearch request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewCustomerServiceClient(conn)
	res, err := client.GetCustomerLawSuit(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "FuzzySearch request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetCustomerTaxIllegal 企业风险-税收违法
func (r *customerRepository) GetCustomerTaxIllegal(ctx context.Context, request *pb.GetCustomerRiskCommonRequest) (*pb.GetCustomerTaxIllegalResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "FuzzySearch request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewCustomerServiceClient(conn)
	res, err := client.GetCustomerTaxIllegal(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "FuzzySearch request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetCustomerRiskCount 企业风险-数量统计
func (r *customerRepository) GetCustomerRiskCount(ctx context.Context, request *pb.GetCustomerRiskCountRequest) (*pb.GetCustomerRiskCountResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "FuzzySearch request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewCustomerServiceClient(conn)
	res, err := client.GetCustomerRiskCount(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "FuzzySearch request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}
